package com.witframework.meta.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.witframework.meta.MetaApp;
import com.witframework.meta.MetaEntity;
import com.witframework.meta.MetaModule;
import com.witframework.meta.service.MetaAppService;
import com.witframework.meta.service.MetaEntityService;
import com.witframework.meta.service.MetaModuleService;
import com.witframework.web.mvc.Controller;
import com.witframework.web.mvc.WebContext;
import com.witframework.web.tree.TreeItem;
import com.witframework.web.tree.TreeNode;
import com.witframework.web.tree.TreeUserdata;

@Component
public class MetaAppTreeController implements Controller{
	@Autowired
	private MetaAppService metaAppService;
	@Autowired
	private MetaModuleService metaModuleService;
	@Autowired
	private MetaEntityService metaEntityService;
	
	public String executor(WebContext webContext) {
		String nodeId = webContext.getHttpRequest().getParameter("id");
		TreeNode treeNode = new TreeNode();
		if(nodeId==null){
			nodeId="root_0";				
		}
		TreeItem root = new TreeItem(nodeId, "root");
		root.setTooltip("this is root node");
		treeNode.setRootNode(root);
		if(nodeId.startsWith("0")){			
			List<MetaApp> appList=metaAppService.getAll();
			for(MetaApp app:appList){
				TreeItem childMenuItem = new TreeItem("app_"+app.getId(),app.getName());				
				childMenuItem.setChild(app.getModules().size());
				childMenuItem.setImg0("application.jpg");
				childMenuItem.setImg1("application.jpg");
				childMenuItem.setImg2("application.jpg");
				TreeUserdata ud=new TreeUserdata("action","metaModuleController.wit?app_id="+app.getId());
				childMenuItem.addUserdata(ud);
				treeNode.addItem(childMenuItem);
				if(appList.size()==1)
					childMenuItem.setOpen(true);
				else
					childMenuItem.setOpen(false);
			}
		}
		else if(nodeId.startsWith("app_")){
			MetaApp app=(MetaApp) metaAppService.get(nodeId.substring(4));
			if(app!=null)
			for(MetaModule module:app.getModules()){
				List<MetaEntity> entityMetaList=metaEntityService.findBy("module",module.getId());
				TreeItem childMenuItem = new TreeItem("module_"+module.getId(),module.getName());
				childMenuItem.setChild(entityMetaList.size());
				childMenuItem.setImg0("module.jpg");
				childMenuItem.setImg1("module.jpg");
				childMenuItem.setImg2("module.jpg");
				TreeUserdata ud=new TreeUserdata("action",webContext.getContextPath()
						+ "/classpath:/com/witframework/meta/view/Unit/wizard1.jsp.wit?app_id="
						+app.getId()+"&module="+module.getId());
				childMenuItem.addUserdata(ud);				
				treeNode.addItem(childMenuItem);	
				if(app.getModules().size()==1)
					childMenuItem.setOpen(true);
				else
					childMenuItem.setOpen(false);
			}
		}else if(nodeId.startsWith("module_")){
			MetaModule module=(MetaModule) metaModuleService.get(nodeId.substring(7));
			List<MetaEntity> entityMetaList=metaEntityService.findBy("module",nodeId.substring(7));
			for(MetaEntity entityMeta:entityMetaList){
				String nodeText=entityMeta.getDisplayName();
				TreeItem childMenuItem = new TreeItem(entityMeta.getId(),nodeText!=null?nodeText:entityMeta.getClassName());	
				childMenuItem.setChild(0);
				childMenuItem.setImg0("//db_table.jpg");
				childMenuItem.setImg1("//db_table.jpg");
				childMenuItem.setImg2("//db_table.jpg");	
				TreeUserdata ud=new TreeUserdata("action",webContext.getContextPath()
						+ "/metaEntityController!edit.wit?id="+entityMeta.getId()+"&app_id="+module.getMetaApp().getId()+"&module="+entityMeta.getModule());
				childMenuItem.addUserdata(ud);
				treeNode.addItem(childMenuItem);		
				
			}
		}
		String xml=treeNode.outString();
		webContext.renderXml(xml);
		return null;
	}
	

}
